﻿using Halfpipe.Data.Repository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data.Objects;
using Halfpipe.Test.Data;

namespace Halfpipe.Test
{
    /// <summary>
    ///This is a test class for ObjectContextExtensionsTest and is intended
    ///to contain all ObjectContextExtensionsTest Unit Tests
    ///</summary>
    [TestClass]
    public class ObjectContextExtensionsTest
    {
        [TestMethod]
        public void GetEntitySetName_returns_correct_value_for_Employee_entity()
        {
            using (ObjectContext context = new NorthwindEntities())
            {
                Type entityType = typeof(Employee);
                string expected = "Employees";

                string actual = ObjectContextExtensions.GetEntitySetName(context, entityType);

                Assert.AreEqual(expected, actual);
            }
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetEntitySetName_throws_when_context_argument_null()
        {
            Type entityType = typeof(Employee);

            string actual = ObjectContextExtensions.GetEntitySetName(null, entityType);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetEntitySetName_throws_when_entityType_argument_null()
        {
            using (ObjectContext context = new NorthwindEntities())
            {
                string actual = ObjectContextExtensions.GetEntitySetName(context, null);
            }
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetEntitySetName_throws_when_entityType_argument_invalid()
        {
            using (ObjectContext context = new NorthwindEntities())
            {
                string actual = ObjectContextExtensions.GetEntitySetName(context, typeof(int));
            }
        }

        [TestMethod]
        public void EntityIsDetached_returns_false_for_attached_entity()
        {
            using (var context = new NorthwindEntities())
            {
                var entity = new Employee();
                context.AddToEmployees(entity);
                bool expected = false;

                bool actual = ObjectContextExtensions.EntityIsDetached(context, entity);

                Assert.AreEqual(expected, actual);
            }
        }

        [TestMethod]
        public void EntityIsDetached_returns_true_for_detached_entity()
        {
            using (ObjectContext context = new NorthwindEntities())
            {
                object entity = new Employee();
                bool expected = true;

                bool actual = ObjectContextExtensions.EntityIsDetached(context, entity);

                Assert.AreEqual(expected, actual);
            }
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void EntityIsDetached_throws_when_context_argument_null()
        {
            object entity = new Employee();

            ObjectContextExtensions.EntityIsDetached(null, entity);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void EntityIsDetached_throws_when_entity_argument_null()
        {
            using (ObjectContext context = new NorthwindEntities())
            {
                ObjectContextExtensions.EntityIsDetached(context, null);
            }
        }
    }
}
